<template>
    <div class='approval'>
      <el-card class="box-card">
        <div class="head-title2">
          <div class='title'>
            {{$t('achievements.title1')}}
          </div>
          <div class="info">
            <el-tooltip placement="left">
              <div slot="content">
                {{define}}
              </div>
              <div class="btns"></div>
            </el-tooltip>
          </div>
        </div>
        <div class='menu-search'> 
            <div class='menu-title'>
              <!-- 选择时间 -->
              <div class='searchItem searchItem-date'>
                <span>{{$t('OverdueCustomerList.chocieDate')}}</span>
                <el-date-picker
                  size='small'
                  v-model="listQuery.startDate"
                  type="date"
                  value-format="yyyy-MM-dd"
                  :placeholder="$t('rule.beginDate')">
                </el-date-picker>
                <b>{{$t('approval.go')}}</b>
                <el-date-picker
                size='small'
                  v-model="listQuery.endDate"
                  type="date"
                  value-format="yyyy-MM-dd"
                  :placeholder="$t('rule.endDate')">
                </el-date-picker>
              </div>
              <!-- APP渠道 -->
              <div class="searchItem">
                <span>{{$t('OverdueCustomerList.channel')}}</span>
                <el-select v-model='listQuery.channel' size='small'>
                    <el-option
                    v-for='item in appChannel'
                    :key='item.channel'
                    :label='item.org_name'
                    :value='item.channel'>
                    </el-option>
                </el-select>
              </div>
            </div>
            <div class='menu-title-btn'>
                <button type='button' @click='search'>
                    {{$t('btn.query')}}
                </button>
                <button type='button' @click='reset'>
                    {{$t('btn.reset')}}
                </button>
                <button type='button' @click="handleDownload" :disabled="expDis" v-if="overdueCustomerListPage_btn_export">
                    {{$t('btn.export')}}
                </button>
            </div>
        </div>
      </el-card>
        <el-card class='box-card' style="margin-top: 10px;margin-bottom: 20px;">
          <div class='title'>
              {{$t('OverdueCustomerList.title2')}}
          </div>
          <el-table
              v-loading="listLoading"
              ref='multipleTable'
              :data='tableData'
              tooltip-effect='dark'
              style='width: 100%'
              @selection-change='handleSelectionChange'
              border
              size='mini'>
              <!-- 时间 -->
              <el-table-column
                :label="$t('OverdueCustomerList.col1')"
                header-align='center'
                prop="crtTime"
                width="150"
                align='center'>
              </el-table-column>
              <!-- S1(1-7) -->
              <el-table-column
              header-align='center'
              align='center'
              prop='agingS1'
              :label="$t('OverdueCustomerList.col2')">
              </el-table-column>
              <!-- S2(8-15) -->
              <el-table-column
              header-align='center'
              align='center'
              prop='agingS2'
              :label="$t('OverdueCustomerList.col3')">
              </el-table-column>
              <!-- S3(16-30) -->
              <el-table-column
              header-align='center'
              align='center'
              prop='agingS3'
              :label="$t('OverdueCustomerList.col4')">
              </el-table-column>
              <!-- M1+(31以上) -->
              <el-table-column
              header-align='center'
              align='center'
              prop='agingM1'
              :label="$t('OverdueCustomerList.col5')">
              </el-table-column>
              <!-- 首逾率 -->
              <el-table-column
              header-align='center'
              align='center'
              prop='firstOverdueRate'
              :label="$t('OverdueCustomerList.col6')">
              </el-table-column>
          </el-table>
          <div class="pagination-container">
            <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="listQuery.page"
            :page-sizes="[5, 10, 20, 30]"
            :page-size="listQuery.limit"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
            </el-pagination>
          </div>
        </el-card>
    </div>
</template>

<script>
import { mapGetters } from 'vuex';
import { getChannels } from 'api/customQuery/index'
import { getRiskOverdueCustomerPage, getRiskOverdueCustomerExport } from 'api/RiskManager/OverdueCustomerList/index'
export default {
  data() {
    return {
      define: '',
      overdueCustomerListPage_btn_export: false,
      totalTrail: '',
      totalPass: '',
      totalPassPercent: '',
      totalOverDualPercent: '',
      listLoading: false,
      total: null,
      expDis: true,
      appChannel: [],
      listQuery: {
        limit: 5,
        page: 1,
        // 开始时间
        startDate: '',
        // 结束时间
        endDate: '',
        // app channel
        channel: '',
        // 查询类型 1实时 2历史
        queryType: 2
      },
      tableData: [],
      multipleSelection: []
    };
  },
  computed: {
    ...mapGetters(['elements', 'overdueCustomerListPage', 'getReportDefine'])
  },
  created() {
    if (this.overdueCustomerListPage.has(this.$route.path)) {
      this.listQuery = this.overdueCustomerListPage.get(this.$route.path)
    }
    getChannels().then(res => {
      if (res && res.status === 200) {
        const data = res.data
        this.appChannel = this.appChannel.concat(data);
        this.listQuery.channel = this.appChannel[0] && this.appChannel[0].channel
        this.getList();
      }
    })
    this.overdueCustomerListPage_btn_export = this.elements['overdueCustomerListDay:btn_export'];
    // 获取报表定义
    this.getReportDefine.forEach(item => {
      if (item.dictCode === 'OVERDUE_CUSTOMER_REPORT') {
        this.define = item.dictValue
      }
    })
  },
  methods: {
    // 点击导出
    handleDownload() {
      this.listLoading = true;
      this.listQuery.queryType = this.listQuery.startDate === '' ? 1 : 2;
      getRiskOverdueCustomerExport(this.listQuery)
      .then(res => {
        require.ensure([], () => {
          const { export_json_to_excel } = require('@/vendor/Export2Excel')
          const tHeader = [this.$t('OverdueCustomerList.crtTime'), this.$t('OverdueCustomerList.col1'), this.$t('OverdueCustomerList.col2'), this.$t('OverdueCustomerList.col3'), this.$t('OverdueCustomerList.col4'), this.$t('OverdueCustomerList.col5'), this.$t('OverdueCustomerList.col6')];
          const filterVal = ['crtTime', 'agingS1', 'agingS2', 'agingS3', 'agingM1', 'firstOverdueRate'];
          const list = res.data;
          const data = this.formatJson(filterVal, list)
          export_json_to_excel(tHeader, data, this.$t('OverdueCustomerList.tabLabel1'))
          this.listLoading = false;
        })
      })
    },
    formatJson(filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => v[j]))
    },
    getList() {
      this.listLoading = true;
      this.$store.dispatch('saveOverdueCustomerListPage', { path: this.$route.path, listQuery: this.listQuery });
      this.listQuery.queryType = this.listQuery.startDate === '' ? 1 : 2;
      getRiskOverdueCustomerPage(this.listQuery)
        .then(res => {
          if (res && res.status === 200) {
            const data = res.data;
            const rows = data.rows;
            this.expDis = false;
            this.listLoading = false;
            this.total = parseInt(data.total || 0);
            this.tableData = rows;
          }
        })
    },
    // 查询
    search() {
      // 时间比较
      this.listQuery.page = 1;
      this.getList();
    },
    // 重置
    reset() {
      this.listQuery.limit = 5;
      this.listQuery.page = 1;
      this.listQuery.startDate = '';
      this.listQuery.endDate = '';
      this.listQuery.channel = this.appChannel[0] && this.appChannel[0].channel
    },
    // 选中的数据
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    handleSizeChange(val) {
      this.listQuery.limit = val;
      this.getList()
    },
    handleCurrentChange(val) {
      this.listQuery.page = val;
      this.getList()
    }
  }
};
</script>

<style scoped lang='scss'>
.title {
  line-height: 30px;
  font-size: 16px;
  display: flex;
  align-items: center;
  font-weight: bold;
}
.title:before{
  display: block;
  content: "";
  width: 3px;
  height: 16px;
  background-color: #309afe;
}
.title:before {
  margin-right: 8px;
}
::before {
  box-sizing: inherit;
  -webkit-tap-highlight-color: transparent;
}
.menu-title {
  width: 100%;
  box-sizing: border-box;
  background: #ffffff;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  .searchItem {
    display: flex;
    align-items: center;
    padding: 10px  0;
    span {
      display: inline-block;
      width: 100px;
      text-align: right;
      font-size: 12px;
      color: #5f5f61;
      padding-right: 5px;
    }
    div:nth-child(2) {
      width: 150px;
    }
  }
  .searchItem-date {
    display: flex;
    align-items: center;
    padding: 10px  0;
    b {
      margin: 0 10px;
      font-weight: normal;
      font-size: 12px;
      color: #5f5f61;
    }
    div {
       width: 150px;
    }
  }
}
.menu-title-btn {
  padding: 10px 0;
  box-sizing: border-box;
  button {
    padding: 0 20px;
    height: 28px;
    background: #20a0ff;
    color: #fff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
    margin-left: 10px;
    &:nth-child(1) {
      margin-left: 0;
    }
    &:nth-child(2) {
      background: #ebf5ff;
      color: #20a0ff;
      border: 1px solid #abd5ff;
    }
  }
  button[disabled] {
    padding: 0 20px;
    height: 28px;
    background: #c1e0ff;
    color: #fafbff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
  }
}
.menu-list {
  padding: 20px 0;
  box-sizing: border-box;
  button {
    padding: 0 20px;
    height: 28px;
    background: #20a0ff;
    color: #fff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
    margin-left: 10px;
    &:nth-child(1) {
      margin-left: 0;
    }
  }
  button[disabled] {
    padding: 0 20px;
    height: 28px;
    background: #c1e0ff;
    color: #fafbff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
  }
}
.head-title2 {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.btns {
  display: inline-block;
  width: 24px;
  height: 24px;
  background-size: cover;
  background-image: url("../../../../assets/images/question-mark.svg");
}
</style>